import time
from scons_symbian import *

constants.SIGNSIS_OUTPUT_EXTENSION = ".sisx"
gcce.GCCE_OPTIMIZATION_FLAGS = "-O3 -fno-unit-at-a-time -ffast-math"

SVNREVISION = ""

#-------------------------------------------------------------------Get SVN info
try:
    import pysvn
    SVNCLIENT = pysvn.Client()
    SVNREVISION = "SVN-%d" % ( SVNCLIENT.info(".")["revision"].number )
except ImportError:
    pass

#-------------------------------------------------------------Initialize globals

DATE = list(time.gmtime()[:3])    
DATE = [ str(x).zfill(2) for x in DATE ]
DATE = "".join( DATE )    
 
#: Name of the title on installer.
PACKAGE_TITLE = ARGUMENTS.get( "sisappname", "pygame for S60" )

#: Name of the python dll to use
PYTHON_LIB_NAME = ARGUMENTS.get( "pythondll", "pygame_python" )

#: Path to the libraries of pygame
PATH_PY_LIBS = ARGUMENTS.get( "pylibs", "data/pygame/libs" )

#: Name of the used sdl.dll ( pygame_sdl.dll )
SDL_DLL_NAME = ARGUMENTS.get( "sdldll", "pygame_sdl" )

#: Name of the used jpeg.dll
JPEG_DLL_NAME = ARGUMENTS.get( "jpegdll", "pygame_libjpeg" )

#: Name of the used libpng.dll
PNG_DLL_NAME = ARGUMENTS.get( "pngdll", "pygame_libpng" )

#: Create static modules or dynamic modules( pyds )
HAVE_STATIC_MODULES = eval( ARGUMENTS.get( "staticmods", False ) )

#: Using OpenC or estlib?
USE_OPENC = ARGUMENTS.get( "openc", True )
if USE_OPENC:
    C_LIBRARY = ["libc", "libm"]
    C_INCLUDE = join( EPOC32_INCLUDE, "stdapis" )
else:
    C_LIBRARY = ["estlib"]
    C_INCLUDE = join( EPOC32_INCLUDE, "libc" )

CAPABILITIES  = ARGUMENTS.get( "capabilities", "").split("+")
if len(CAPABILITIES) == 0:
    CAPABILITIES = CAPS_SELF_SIGNED
    constants.SIGNSIS_OUTPUT_EXTENSION = "selfsigned.sisx"

SISVERSION     = ARGUMENTS.get( "sisversion", "1,0,0").split(",")
PYTHON_INCLUDE = ARGUMENTS.get( "pythoninclude", EPOC32_INCLUDE + "/python")
PYTHON_VERSION = PYTHON_INCLUDE[-2:] # epoc32/include/python25
if not PYTHON_VERSION.isdigit(): # But missing on pre 1.9.x PyS60( Python 2.2 )
    PYTHON_VERSION = "22"

# Pygame's installer package
PACKAGE_NAME = ARGUMENTS.get( "package", "pygame-S60_py%(PYVER)s_%(REVISION)s_%(DATE)s_%(COMPILER)s-%(RELEASE)s.sis" % \
                            {   "DATE"     : DATE, 
                                "REVISION" : SVNREVISION, 
                                "COMPILER" : COMPILER.upper(), 
                                "RELEASE"  : RELEASE.upper(),
                                "PYVER"    : PYTHON_VERSION 
                            } )

# SIS certificate 
CERT = ARGUMENTS.get("cert", None)
KEY  = ARGUMENTS.get("privkey", None)
PASSPHRASE  = ARGUMENTS.get("passphrase", "" )

#-----------------------------------------------------------------------Get UIDs     
UID_PACKAGE    = ARGUMENTS["sisuid"] 
UID_PYGAMEAPP  = ARGUMENTS["appuid"] 
UID_SDL        = ARGUMENTS["sdluid"] 
UID_JPEG       = ARGUMENTS["jpeguid"]
UID_PNG        = 0#ARGUMENTS["pnguid"]
#: The base uid for all pygame modules. 
UID_MODBASE    = ARGUMENTS["modbaseuid"]
__uid = int(UID_MODBASE, 16)
def getUID(): 
    global __uid
    __uid += 1
    return __uid 

EMBEDDED_PYTHON_SIS = ARGUMENTS.get( "pythonsis", "none" )
if EMBEDDED_PYTHON_SIS.lower() != "none" and COMPILER != COMPILER_WINSCW:
    # Read the UID
    f=open( EMBEDDED_PYTHON_SIS,'rb')
    # First 16 bytes are for UIDs
    # See: http://homepage.ntlworld.com/thouky/software/psifs/sis.html
    data=f.read(16)
    f.close()
    import struct
    uids = struct.unpack("IIII", data )
    EMBEDDED_PYTHON_SIS_UID = hex(uids[2]).replace("L","")
    print "Read UID %s from '%s'" % ( EMBEDDED_PYTHON_SIS_UID, EMBEDDED_PYTHON_SIS  )
else:
    EMBEDDED_PYTHON_SIS = None
    EMBEDDED_PYTHON_SIS_UID = 0x0
      
Export("PACKAGE_NAME PYTHON_LIB_NAME"
       " PATH_PY_LIBS SDL_DLL_NAME"
       " CAPABILITIES PYTHON_INCLUDE"
       " USE_OPENC C_LIBRARY C_INCLUDE"
       " HAVE_STATIC_MODULES getUID")

# Build external libraries. Conditional because needed only once and takes extra time after that.
if eval( ARGUMENTS.get( "build_libs", "True" ) ):    
    SConscript( "SConscript.sft2.py" )
    SConscript( "SConscript.Ogg.py" )    
    SConscript( "SConscript.Vorbis.py" )
    
    UID3 = UID_JPEG
    TARGET_NAME = JPEG_DLL_NAME  
    Export("TARGET_NAME UID3")
    SConscript( "SConscript.jpeg.py" )  
    
    UID3 = UID_PNG
    TARGET_NAME = PNG_DLL_NAME  
    Export("TARGET_NAME UID3")
    SConscript( "SConscript.libpng.py" )  
    
    TARGET_NAME = SDL_DLL_NAME  
    UID3        = UID_SDL     
    Export( "TARGET_NAME UID3" )
    SConscript( "SConscript.SDL.py" )
    
# Build pygame.lib
SConscript( "SConscript.pygame.py")

# Build pygame application
Export("UID_PYGAMEAPP CAPABILITIES")

SConscript( "SConscript.app.py")

#===============================================================================
# Create the package
#===============================================================================
pkgtemplate = "pygame_pkgtemplate.txt"

# Python is embedded as a sis
pkgargs = {
    "version" : SISVERSION,
    "uid"     : UID_PACKAGE,
    "appname" : PACKAGE_TITLE,
    'cert':      CERT,
    'key':       KEY,
    'passwd':    PASSPHRASE,
    "EMBEDDED_PYTHON_SIS"     : EMBEDDED_PYTHON_SIS, 
    "EMBEDDED_PYTHON_SIS_UID" : EMBEDDED_PYTHON_SIS_UID,
}

Depends( PACKAGE_NAME, EMBEDDED_PYTHON_SIS )
SymbianPackage(PACKAGE_NAME, pkgargs=pkgargs, pkgtemplate = pkgtemplate )


